import {
  signin,
  todaySignin
} from "../../common/api/share"
// packageA/signIn/signIn.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        name: '周一',
        dayOfWeek: 1,
        num: 10,
        type: 4
      },
      {
        name: '周二',
        dayOfWeek: 2,
        num: 10,
        type: 4
      },
      {
        name: '周三',
        dayOfWeek: 3,
        num: 10,
        type: 4
      },
      {
        name: '周四',
        dayOfWeek: 4,
        num: 10,
        type: 4
      },
      {
        name: '周五',
        dayOfWeek: 5,
        num: 10,
        type: 4
      },
      {
        name: '周六',
        dayOfWeek: 6,
        num: 10,
        type: 4
      }, {
        name: '周日',
        dayOfWeek: 7,
        num: 10,
        type: 4
      },

    ],
    isSignIn: false,
    dayOfWeek: 1,
    gold: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  // 领取奖励
  async getAward() {
    if(this.data.isSignIn){
      wx.showToast({
        title: '您已签过到，要等明天才能继续签到哦~~~',
        icon: "none"
      })
      return
    }
    wx.showLoading({})
    let arr = JSON.parse(JSON.stringify(this.data.list));
    let user = wx.getStorageSync('userInfo');
    let res = await signin({
      userId: user.id
    });
    wx.hideLoading();
    wx.showToast({
      title: '签到成功~~~',
      icon: "none"
    })
    // arr.forEach(e => {
    //   if (e.dayOfWeek == this.data.dayOfWeek) {
    //     e.type = 2;
    //   }
    // }) 
    
    this.setData({
      isSignIn: true,
      list: arr
    });
    this.getData();
  },
  // 请求用户数据
  async getData() {
    let user = wx.getStorageSync('userInfo');
    let res = await todaySignin({
      userId: user.id
    });
    let arr = JSON.parse(JSON.stringify(this.data.list));
    let IsSignIn = false;
    // res.dayOfWeek = 5; 
    arr.forEach(e => {
      /**
       * 
       *  type: 
       *  1 过去的时间
       * 2 未签到
       * 3 已签到
       *  4 未来的时间
       */
      if (e.dayOfWeek < res.dayOfWeek) {
        e.type = 1;
      } else if (e.dayOfWeek == res.dayOfWeek) {
        e.type = res.today ? 2 : 3;
        IsSignIn = res.today ? true : false;
        this.data.dayOfWeek = res.dayOfWeek;
      }

    });
    this.setData({
      list: arr,
      isSignIn: IsSignIn,
      gold: res.gold
    });
  },
  // 跳转到兑换界面
  goConversion() {
    // wx.switchTab({
    //   url: "/pages/announce/announce",
    // })
    wx.navigateTo({
      url: '/pages/personalCenter/rechargeAmount/rechargeAmount',
    })
  }
})